<?php
/**
 * 个人资料
 * @copyright (c) Seer All Rights Reserved
 */

require_once 'globals.php';
if ($action == '') {
	$User_Model = new User_Model();
	$row = $User_Model->getOneUser(UID);
	extract($row);
	$icon = '';
	if ($photo) {
		$imgsize = chImageSize($photo, Option::ICON_MAX_W, Option::ICON_MAX_H);
        $token = LoginAuth::genToken();
		$icon = "<img src=\"{$photo}\" width=\"{$imgsize['w']}\" height=\"{$imgsize['h']}\" style=\"border:1px solid #CCCCCC;padding:1px;\" />
		<br /><a href=\"javascript: em_confirm(0, 'avatar', '$token');\">删除头像</a>";
	} else {
		$icon = '<img src="./views/images/avatar.jpg" />';
	}
	include View::getView('header');
	require_once(View::getView('admin_config'));
	include View::getView('footer');
	View::output();
}

if ($action == 'update') {
    LoginAuth::checkToken();
	$User_Model = new User_Model();
	$description = isset($_POST['description']) ? addslashes(trim($_POST['description'])) : '';
    $login = isset($_POST['username']) ? addslashes(trim($_POST['username'])) : '';
	$newpass = isset($_POST['newpass']) ? addslashes(trim($_POST['newpass'])) : '';
	$repeatpass = isset($_POST['repeatpass']) ? addslashes(trim($_POST['repeatpass'])) : '';
    if (strlen($newpass)>0 && strlen($newpass) < 6) {
        seerDirect("./admin_config.php?error_c=1");
	} elseif (!empty($newpass) && $newpass != $repeatpass) {
        seerDirect("./admin_config.php?error_d=1");
	} elseif($User_Model->isUserExist($login, UID)) {
        seerDirect("./admin_config.php?error_e=1");
    } elseif($User_Model->isNicknameExist($nickname, UID)) {
        seerDirect("./admin_config.php?error_f=1");
    }

	if (!empty($newpass)) {
        $PHPASS = new PasswordHash(8, true);
		$newpass = $PHPASS->HashPassword($newpass);
		$User_Model->updateUser(array('password'=>$newpass), UID);
	}
	$CACHE->updateCache('user');
	seerDirect("./admin_config.php?active_edit=1");
}
